package com.dd.admin.business.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.memberCard.entity.MemberCard;
import com.dd.admin.business.pay.domain.BuildCardSell;
import com.dd.admin.business.pay.domain.ChargedCardSell;
import com.dd.admin.business.pay.domain.MemberProductSell;
import com.dd.admin.business.pay.service.AddSellDetailService;
import com.dd.admin.business.pay.service.PayService;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.business.sell.service.SellService;
import com.dd.admin.business.sellJson.entity.Selljson;
import com.dd.admin.business.sellJson.service.SelljsonService;
import com.dd.admin.business.sellPay.service.SellpayService;
import com.dd.admin.common.security.SecurityUtil;
import com.dd.admin.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import static com.dd.admin.business.pay.enmus.DetailServiceEnum.*;
import static com.dd.admin.business.pay.enmus.PayConst.*;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private SellService sellService;

    @Autowired
    private Map<String, AddSellDetailService> detailServiceMap;

    @Autowired
    private SellpayService sellpayService;

    @Autowired
    private SelljsonService selljsonService;

    @Override
    public Sell createSell(Sell sell) {
        String shopId = SecurityUtil.getLoginUser().getDeptId();
        //直接执行save即可
        if(StringUtil.isEmpty(sell.getSellNo())){
            String sellNo = (StringUtil.getDateStringNow() + StringUtil.createCode(4));
            sell.setSellNo(sellNo);
        }

        //插入时查看该门店今天数量为流水号
        Integer count = sellService.selectTodayCount(shopId);
        sell.setSerialNo(String.valueOf(count + 1));
        sellService.save(sell);
        return sell;
    }

    @Override
    public Sell setSellExtraInfo(String sellId, MemberCard memberCard, BigDecimal memberBeforeAmount, BigDecimal memberBeforeGiveAmount) {
        Sell extraSellInfo = new Sell();
        extraSellInfo.setSellId(sellId);
        if(memberCard!=null){
            //设置订单的卡信息
            extraSellInfo.setMemberCardId(memberCard.getMemberCardId());
            extraSellInfo.setMemberCardName(memberCard.getCardName());
            extraSellInfo.setMemberCardNo(memberCard.getCardNo());
            //设置订单的卡余额
            extraSellInfo.setMemberCardBeforeAmount(memberBeforeAmount);
            extraSellInfo.setMemberCardBeforeGiveAmount(memberBeforeGiveAmount);

            extraSellInfo.setMemberCardAfterAmount(memberCard.getAmount());
            extraSellInfo.setMemberCardAfterGiveAmount(memberCard.getGiveAmount());
        }
        sellService.updateById(extraSellInfo);
        return extraSellInfo;
    }

    @Override
    public Sell executeBuildCard(BuildCardSell buildCardSell) {
        if(StringUtil.isNotEmpty(buildCardSell.getJsonId())){
            selljsonService.completePendingSell(buildCardSell.getJsonId());
        }

        Integer sellType = SELL_TYPE_BUILD_CARD;
        //第一步创建订单
        Sell sell = BeanUtil.copyProperties(buildCardSell,Sell.class);
        sell.setSellType(sellType);

        BigDecimal realAmount = sellpayService.getRealAmount(buildCardSell.getSellPayList(), buildCardSell.getSellAmount());
        sell.setRealAmount(realAmount);
        createSell(sell);
//        selljsonService.save(new Selljson().setSellId(sell.getSellId()).setSellJson(buildCardSell.getSellJson()));
        //创建销售细单
        detailServiceMap.get(BUILD_CARD.getServiceName()).addSellDetail(sell,buildCardSell);
        return sell;
    }

    @Override
    public Sell executeChargedCard(ChargedCardSell chargedCardSell) {
        if(StringUtil.isNotEmpty(chargedCardSell.getJsonId())){
            selljsonService.completePendingSell(chargedCardSell.getJsonId());
        }



        Integer sellType = SELL_TYPE_CHARGED_CARD;
        //chargedCardSell
        Sell sell = BeanUtil.copyProperties(chargedCardSell,Sell.class);
        sell.setSellType(sellType);

        BigDecimal realAmount = sellpayService.getRealAmount(chargedCardSell.getSellPayList(), chargedCardSell.getSellAmount());
        sell.setRealAmount(realAmount);
        createSell(sell);
//        selljsonService.save(new Selljson().setSellId(sell.getSellId()).setSellJson(chargedCardSell.getSellJson()));
        //创建销售细单
        detailServiceMap.get(CHARGED_CARD.getServiceName()).addSellDetail(sell,chargedCardSell);
        return sell;
    }

    @Override
    public Sell executeProduct(MemberProductSell productSell) {
        if(StringUtil.isNotEmpty(productSell.getJsonId())){
            selljsonService.completePendingSell(productSell.getJsonId());
        }


        Integer sellType = SELL_TYPE_PRODUCT;
        //第一步创建订单
        Sell sell = BeanUtil.copyProperties(productSell,Sell.class);
        sell.setSellType(sellType);

        BigDecimal realAmount = sellpayService.getRealAmount(productSell.getSellPayList(), productSell.getSellAmount());
        sell.setRealAmount(realAmount);
        createSell(sell);
        //创建销售细单
        detailServiceMap.get(MEMBER_PRODUCT.getServiceName()).addSellDetail(sell,productSell);
        return sell;
    }


}
